﻿#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMessageBox>
#include <QFileDialog>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

 // 初始化OpenCL
    m_oclManager = new OpenCLManager(this);
    if(!m_oclManager->initialize()) {
        QMessageBox::warning(this, "OpenCL Error", "Failed to initialize OpenCL");
    } else {
        ui->deviceInfoLabel->setText(m_oclManager->getDeviceInfo());
    }

    // 初始化图像处理器
    m_imageProcessor = new ImageProcessor(m_oclManager, this);
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    if(m_originalImage.isNull()) {
        QMessageBox::warning(this, "Error", "No image loaded");
        return;
    }

    m_processedImage = m_imageProcessor->processImage(m_originalImage);
    ui->processedLabel->setPixmap(QPixmap::fromImage(m_processedImage).scaled(
        ui->processedLabel->size(), Qt::KeepAspectRatio));
}

void MainWindow::on_pushButton_2_clicked()
{
    if(m_processedImage.isNull()) {
        QMessageBox::warning(this, "Error", "No processed image to save");
        return;
    }

    QString fileName = QFileDialog::getSaveFileName(
        this, "Save Image", "", "Images (*.png *.jpg *.bmp)");

    if(!fileName.isEmpty()) {
        m_processedImage.save(fileName);
    }
}

void MainWindow::on_pushButton_3_clicked()
{
    QString fileName = QFileDialog::getOpenFileName(
        this, "Open Image", "", "Images (*.png *.jpg *.bmp)");

    if(!fileName.isEmpty()) {
        m_originalImage.load(fileName);
        ui->originalLabel->setPixmap(QPixmap::fromImage(m_originalImage).scaled(
            ui->originalLabel->size(), Qt::KeepAspectRatio));
    }
}

